<!--
	作者：392019883@qq.com
	时间：2018-08-10
	描述：过站盘点前端程序
-->
<!DOCTYPE html>
<html lang="zh-cmn-Hans">

	<head>
		<meta http-equiv="Expires" content="0" />
		<meta http-equiv="Pragma" content="no-cache" />
		<meta http-equiv="Cache-control" content="no-cache" />
		<meta http-equiv="Cache" content="no-cache" />
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
		<title>生产过站盘点程序</title>
		<link rel="stylesheet" href="../layui/css/layui.css" />
	</head>

	<body>
		<fieldset class="layui-elem-field site-demo-button">
			<legend><big><b>生产过站盘点程序</b></big></legend>
			<div class="layui-fluid layui-form layui-form-pane">
				<div class="layui-row layui-form-item">
					<label class="layui-form-label">扫描二维码:</label>
					<div class="layui-input-block">
						<input class="layui-input" placeholder="流程：员工号→流程单→保存过站" id="qr" type="text" />
					</div>
				</div>
			</div>
		</fieldset>
		<fieldset class="layui-elem-field site-demo-button">
			<legend><big><b>这里是当前扫描的信息组合</b></big></legend>
			<div id="DQ" class="layui-fluid layui-form layui-form-pane">
				注:<big><b>※</b></big>号为必扫项目
				<div class="layui-row">
					<div class="layui-col-sm6">
						<label class="layui-form-label"><big><b>※</b></big>员工工号:</label>
						<div class="layui-input-block">
							<input class="layui-input" readonly=true placeholder="这里是必填项※员工工号(请扫入工号)" id="YGGH" type="text" />
						</div>
					</div>
					<div class="layui-col-sm6">
						<label class="layui-form-label">员工姓名:</label>
						<div class="layui-input-block">
							<input type="hidden" id="dqsjkps" />
							<input class="layui-input" readonly=true placeholder="这里是员工姓名(根据工号自动产生)" id="YGXM" type="text" />
						</div>
					</div>
				</div>
				<div class="layui-row">
					<div class="layui-col-sm6">
						<label class="layui-form-label"><big><b>※</b></big>当前站点:</label>
						<div class="layui-input-block">
							<input class="layui-input" readonly=true placeholder="这里是必填项※当前站点(请扫入站点二维码)" id="DQKX" type="text" />
						</div>
					</div>
					<div class="layui-col-sm6">
						<label class="layui-form-label"><big><b>※</b></big>工单号:</label>
						<div class="layui-input-block">
							<input class="layui-input" readonly=true placeholder="这里是必填项※当前工单号" id="DQGDH" type="text" />
						</div>
					</div>

				</div>
			</div>
		</fieldset>
		<fieldset class="layui-elem-field site-demo-button">
			<legend><big><b>盘点信息(只Show当前站点的信息)</b></big></legend>

			<div class="layui-row">
				<!--加一个隐藏的文本框来存输入的基准片数吧-->
				<input type="hidden" id="jzps" />
				<table class="layui-table lay-even">
					<thead>
						<tr class="table-active">
							<th width="10%" scope="col">工令<br>单号</th>
							<th width="10%" scope="col">基准<br>片数</th>
						</tr>
					</thead>
					<tbody id="blues">
					</tbody>
				</table>
			</div>
			<div class="layui-row layui-btn-container">
				<div class="layui-col-sm4">
					<input type="hidden" id="gdps" />

					<button class="layui-btn  layui-btn-normal" id="keep">保存</button>
				</div>
			</div>
		</fieldset>

		<!-- 加载jQuery文件 -->
		<script src="../js/jquery.min.js"></script>
		<!-- 加载类ui文件 -->
		<script src="../layui/layui.js"></script>
		<!-- 写脚本 -->
		<script>
			var GD = {} //工单信息的字典
			var htmlstr = ""; //网页的文本
			var Dhtml = "";
			var Hhtml = "";
			var JTGD = {}; //这里是机台号对应工单号的数组
			//这里开始写入s2的对照表
			var STR2 = {
				"EM": "YGGH"
				//,"AT":"DQGD"
				//,"BP":"DQJT"
			}
			//载入layui模块：layer弹出层,form表单,element元素组件做分页或其他
			layui.use(['jquery', 'layer', 'form', 'element'], function() {
				var $ = layui.$; //重点处
				var layer = layui.layer;
				var form = layui.form;
				var element = layui.element;
				var table = layui.table;

				//这里是画面准备完毕后执行的程式
				$(document).ready(function() {
					//如果HTML5WEB存储存在,就查询,否则就弹窗提示
					if(localStorage.scyggh) {
						$("#YGGH").val(localStorage.scyggh);
						$.ajax({
							type: 'POST',
							url: "http://172.31.3.27:84/index.php?s=/tdled/Scgzpd/xm", //172.31.3.27:84//127.0.0.1:85,//POST傳一個超長字串到後端
							data: "qrc=" + localStorage.scyggh,
							dataType: "json",
							success: function(sss) {
								if(sss.msg) {
									layer.msg(sss.msg);
								}

								layer.msg(sss['XM'][0]['Name'] + ',欢迎回来!');
								$("#YGXM").val(sss['XM'][0]['Name']); //把姓名单独查出来
								$("#DQKX").val(sss['XM'][0]['GongZhan']); //写工单号

							}
						});
						//yc(); //调用写异常的方法
					} else {
						layer.msg("请扫工号牌");
					}

					$("#qr").select(); //画面准备完毕后就选择扫码输入框
				});

				$("#qr").keypress(function(e) { //这里是扫描输入框的回车按下后执行的程式
					/*
					· 判断输入的是否是数量,是否小于等于20片,通过后修改数量 => 优化呼叫API,然后弹出详情框,让写备注原因
					· 判断是否是进烤 =>新程序不需要
					· 判断是否是出烤 =>新程序不需要
					· 判断是不是门 => 新程序不需要
					· 判断工号
					· 判断机台
					*/
					if(e.keyCode === 13) { //如果按下的键值是回车键13就执行以下语句
						$("#qr").select(); //扫描输入框选择
						var QRC = $("#qr").val(); //定义局部变量QRC=扫描输入框的值
						QRC = QRC.toUpperCase(); //将扫描仪扫到的内容转换为大写
						var s2 = QRC.substr(0, 2);

						var patt1 = /^[A-Z]{1,2}[A-Z\d]*\-?[A-Z\d]?(?= T)/; //AK17B2681-2 -> mg组装规格书工单号
						var patt2 = /\d{3}$/; //002组装规格书盒序号
						var patt3 = /[A-Z]{1,2}[A-Z\d]*\-?[A-Z\d]?(?= T)/; //AK187029C-X分光标签工单号
						var patt5 = /BP[A-Z]P\d{3}/; //机台号或MES机台EQ
						var patt6 = /\d+(?= \d{3}$)/; //组装规格书基准片数
						var patt7 = /^TA/; //兼容,还是要替换掉TA

						var string1 = QRC.replace(patt7, ""); //取出工单号
						var string2 = patt1.exec(string1) + "-" + patt2.exec(string1); //取出工单号盒号、
						var dqps = patt6.exec(QRC);
						$("#gdps").val(dqps);

						if(/^\d{1,2}$/.test(QRC)) {
							var sqty = QRC * 1;
							if(sqty <= 20 && sqty <= $("#dqsjkps").val()) {
								$("#jzps").val(sqty);
								showxgps();
							}else if(!patt1.test(string1) || !patt3.test(string1)){
								layer.msg("请确认工单号是否正确!");
							} else {
								layer.msg("请确认当前料盒的装片上限!");
							}
						} else if(patt1.test(string1) || patt3.test(string1)) { //判断是否为工单号
							if(patt1.test(string1)) { //组装规格书工单号测试通过

								var string2 = patt1.exec(string1) + "-" + patt2.exec(string1); //取出工单号盒号

								$("#DQGDH").val(string2); //写工单号
								//JTGD[$("#DQKX").val()] = string2; //机台工单索引机台=工单号盒号

								if($("#DQKX").val() !== "") { //如果当前机台不为空,就调用函数

									//$("#DQGDH").val(string2);
									//扫描到工单号后就开始进去数据库判断当前工单号下是否有存在状态为1的数据
									var gdhh = $("#DQGDH").val(); //获取工单号和盒号=>工单号-盒号

									$.ajax({
										type: 'POST',
										url: "http://172.31.3.27:84/index.php?s=/tdled/Scgzpd/yz", //172.31.3.27:84//127.0.0.1:85
										data: "yggh=" + $("#YGGH").val() + "&kxh=" + $("#DQKX").val() + "&gdh=" + string2,
										dataType: "JSON",
										success: function(sss) {
											//console.log("hhh");
											if(sss.msg != '大吉大利') {
												layer.msg('<div style="background-color: #FF5722;"><big><b>' + sss.msg + '</b></big></div>');
												if(sss.zt == "outsub") {
													return;
												}
											} else {
												
											}

										}

									});

									$.ajax({
										type: 'POST',
										url: "http://172.31.3.27:84/index.php?s=/tdled/Scgzpd/jzps", //172.31.3.27:84//127.0.0.1:85
										data: "gdh=" + gdhh + "&dqps=" + dqps,
										dataType: "json",
										success: function(sss) {
											
											if(sss.CD>=1){
												
												var dqsjkps = sss['sj'][0]['QTY']; //当前数据库片数
												$("#dqsjkps").val(dqsjkps);
												
												GD[$("#DQGDH").val()] = dqsjkps; //当前工单号的数量被修改,每次存到数组字典中
												layer.msg(JSON.stringify(GD)); //转成JSON文本查错
												$("#blues").html("");
												for(var key in GD) {
													try {
														var value = GD[key];
														htmlstr = "<tr><td>" + key + "</td><td>" + value + "</td></tr>";
														$("#blues").prepend(htmlstr);

													} catch(e) {}

												}
												
												
												
												
											}else{
												
												var dqsjkps = sss['shuju'][0]['QTY']; //当前数据库片数
												$("#dqsjkps").val(dqsjkps);//放到input里没办法更新
												
												GD[$("#DQGDH").val()] = dqsjkps; //当前工单号的数量被修改,每次存到数组字典中
												layer.msg(JSON.stringify(GD)); //转成JSON文本查错
												$("#blues").html("");
												for(var key in GD) {
													try {
														var value = GD[key];
														htmlstr = "<tr><td>" + key + "</td><td>" + value + "</td></tr>";
														$("#blues").prepend(htmlstr);

													} catch(e) {}

												}
												
											}
											

										}
									});

								} else {
									layer.msg('机台号不能为空');
								}

							} else if(patt3.test(QRC)) { //否则它是分光标签
								var string2 = patt3.exec(QRC) + "-" + patt4.exec(QRC); //取出分光标签的工单号
								$("#DQGDH").val(string2); //写分光标签的工单号
								JTGD[$("#DQKX").val()] = string2; //写当前机台的工单号
							} else {
								layer.msg('工单号的正则不通过，请检查！<br/>组装规格书是否是TA开头？<br/>编带站卷盘标签是否包含工单号');
							}
						} else if(patt5.test(QRC)) { //取到工单号就不会跑来取机台号了吧,编带机上面的流水码是BPI分光机自动标签s2==="BP" && $("#GDH").val() !=="")
							var dqjt = patt5.exec(QRC); //当前机台

							$("#DQKX").val(dqjt[0]); //写当前机台
							JTGD[dqjt[0]] = $("#DQGDH").val(); //当前机台的工单号

						} else if(STR2[s2]) {
							$("#" + STR2[s2]).val(QRC); //如果文本2等于

							if(s2 === "EM") { //判断如果是工号就写WEB存储
								localStorage.scyggh = QRC;
								location.reload(); //如果是工号,还要清除字典内容,重新来过...所以干脆刷新一下好了,下面是语句
							}
						} else {
							layer.msg("机台号和工单号不能为空!");
						}
					}

					//显示修改片数的函数
					function showxgps() {

						var ps = $("#jzps").val(); //获取用户输入的基准片数
						var gdhh = $("#DQGDH").val(); //获取工单号和盒号=>工单号-盒号

						var kaichuang = layer.open({
							type: 1,
							offset: true,
							content: "<div class='layui-form'>" +
								"	<div class='layui-form-item'>" +
								"		<label class='layui-form-label'>修改片数：</label>" +
								"		<div class='layui-input-inline'>" +
								"			<input type='text' class='layui-input' style='width:96px' title='基准片数' value='" + ps + "'/>" +
								"		</div>" +
								"	</div>" +
								"	<div class='layui-form-item'>" +
								"		<label class='layui-form-label'>片数：</label>" +
								"		<div class='layui-input-inline'>" +
								"			<input type='text' class='layui-input' style='width:96px' value='" + $("#dqsjkps").val() + "'/>" +
								"		</div>" +
								"	</div>" +
								"	<div class='layui-form-item'>" +
								"		<label class='layui-form-label'>备注：</label>" +
								"		<div class='layui-input-inline'>" +
								"			<textarea class='layui-textarea' id='bz' placeholder='请在这里填写备注'></textarea>" +
								"		</div>" +
								"	</div>" +
								"</div>", //内容
							btn: ['取消', '确认修改'],
							yes: function() {
								layer.close(kaichuang);
								layer.msg("您已取消修改");
								return false;
							},
							btn2: function() {

								var bz = $("#bz").val(); //获取备注

								if(bz != "") {
									$.ajax({
										type: 'POST',
										url: "http://172.31.3.27:84/index.php?s=/tdled/Scgzpd/upjzps", //172.31.3.27:84//127.0.0.1:85
										data: "jzps=" + ps + "&gdh=" + gdhh + "&bz=" + bz,
										dataType: "json",
										success: function(sss) {
											if(sss.yyps >= 1) {
												layer.msg(sss.msg);
											} else {
												$("#dqsjkps").val(ps);//每次修改完片数后就把输入的片数存到页面hidden属性中用来继续修改做比较
												layer.msg(sss.msg);

												layer.close(kaichuang);
												
												GD[$("#DQGDH").val()] = ps; //当前工单号的数量被修改,每次存到数组字典中
												layer.msg(JSON.stringify(GD)); //转成JSON文本查错
												$("#blues").html("");
												for(var key in GD) {
													try {
														var value = GD[key];
														htmlstr = "<tr><td>" + key + "</td><td>" + value + "</td></tr>";
														$("#blues").prepend(htmlstr);

													} catch(e) {}

												}
												
											}

										}
									});
								} else {
									layer.msg("请输入备注！");
									return false;
								}

							}
						});

					}

				});

			});

			$("#qr").click(function() {
				$("#qr").select();
			});

			$("#keep").click(function() {
				var yggh = $("#YGGH").val();
				var dqzd = $("#DQKX").val();
				var gdh = $("#DQGDH").val();
				var gdps = $("#gdps").val();
				if($("#YGGH").val() && $("#DQKX").val() && $("#DQGDH").val()) {
					$.ajax({
						type: 'POST',
						url: "http://172.31.3.27:84/index.php?s=/tdled/Scgzpd/keep", //172.31.3.27:84//127.0.0.1:85
						data: "yggh=" + yggh + "&dqzd=" + dqzd + "&gdh=" + JSON.stringify(GD) + "&gdps" + gdps,
						dataType: "json",
						success: function(sss) {} //没有返回数据，先留着
					});
					layer.msg("保存成功了！");
					$("#blues").html("");
					GD = {};
				} else {
					layui.use(['jquery', 'layer'], function() {
						var $ = layui.$; //重点处
						var layer = layui.layer;
						layer.msg("员工工号或工单号为空时，不能保存！");
					});
				}
			});
		</script>

	</body>

</html>